package org.fuys.coder.application;

import lombok.extern.slf4j.Slf4j;
import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.common.entity.Result;
import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.common.holder.UserHolder;
import org.fuys.coder.domain.auth.model.vo.token.TokenTypeVO;
import org.fuys.coder.domain.auth.service.vertify.token.ITrustTokenService;
import org.fuys.coder.domain.substance.model.req.admin.SubstancePublishReq;
import org.fuys.coder.domain.substance.model.req.operation.SubstanceCategorySelectReq;
import org.fuys.coder.domain.substance.model.vo.SubstanceDetailVO;
import org.fuys.coder.domain.substance.model.vo.SubstanceSupplyVO;
import org.fuys.coder.domain.substance.model.vo.SubstanceVO;
import org.fuys.coder.domain.substance.service.repo.ISubstanceService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.application
 * @className: SubstanceController
 * @author: WangJing
 * @description: 内容控制器 一般是只读 获取内容信息
 * @date: 2024/7/12 17:27
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/fuyscoder/substance")
public class SubstanceController {

    @Resource
    private ISubstanceService substanceService;

    @Resource
    private ITrustTokenService tokenService;

    /**
     * @param substanceId: 内容id
     * @return Result
     * @author WangJing
     * @description 通过id获取全部信息 适用于直接从url中获取内容
     * @date 2024/7/15 11:44
     */
    @GetMapping("/detail/{substanceId}")
    public Result getSubstance(@PathVariable("substanceId") Long substanceId){
        SubstanceDetailVO detailVO= null;
        try{
            detailVO = substanceService.getSubstanceDetail(substanceId);
            if(ObjectUtils.isEmpty(detailVO)){
                return Result.error(ResultMessageConstants.SUBSTANCE_NOT_FOUND);
            }
        }catch (Exception e){
            return Result.error(ResultMessageConstants.SUBSTANCE_NOT_FOUND);
        }
        return Result.ok().data(detailVO);
    }

    /**
     * @param substanceId: 内容id
     * @return Result
     * @author WangJing
     * @description 通过id获取补充信息 因为有些可能前端已经存在了 目前规定只有文件表需要支援
     * @date 2024/7/15 12:04
     */
    @GetMapping("/supplement/{substanceId}")
    public Result getSubstanceSupply(@PathVariable("substanceId") Long substanceId){
        SubstanceSupplyVO supplyVO= null;
        try {
            supplyVO = substanceService.getSubstanceSupply(substanceId);
            if(ObjectUtils.isEmpty(supplyVO)){
                return Result.error(ResultMessageConstants.SUBSTANCE_NOT_FOUND);
            }
        } catch (Exception e) {
            return Result.error(ResultMessageConstants.SUBSTANCE_NOT_FOUND);
        }

        return Result.ok().data(supplyVO);
    }

    @PostMapping("/publish/substance")
    public Result publishSubstance(@RequestBody SubstancePublishReq req){
        try{
            //尝试发布内容 如果存在信息缺失 或是token不合法 则取消交易
            final Integer authorId = UserHolder.get();
            if(ObjectUtils.isEmpty(authorId)){
                throw new BusinessException(ResultMessageConstants.NO_USER_TOKEN);
            }
            req.setAuthorId(authorId);
            substanceService.publishSubstance(req);
        }catch (BusinessException e){
            //返回错误信息
            return Result.error(e.getMsg());
        }
        return Result.ok();
    }

    @GetMapping("/publish/token")
    public Result publishToken(){
        final Integer userId = UserHolder.get();
        if(ObjectUtils.isEmpty(userId)){
            return Result.error(ResultMessageConstants.NO_USER_TOKEN);
        }
        Pair<String[],Long[]> tokenInfo=tokenService.operationToken(userId,null, TokenTypeVO.SUBSTANCE_PUBLISH);
        return Result.ok().data(tokenInfo);
    }

    @PostMapping("/by/category")
    public Result substancesByCategory(@RequestBody SubstanceCategorySelectReq req){
        final List<SubstanceVO> substanceByCategories = substanceService.getSubstanceByCategories(req.getCategoryIds(), req.getPageDTO().getCurPage()
                , req.getPageDTO().getPageNum());
        return Result.ok().data(substanceByCategories);
    }
}
